<?php 
    namespace Admin\Controller;
    
    //经验管理 控制器
    class ClassifyController extends AdminController{

        private $_model = null; //数据库操作类
        private $_role = null; //角色表操作类
        private $_user_role = null; //用户——角色表操作类

        //初始化操作
        public function _initialize(){
            parent::_initialize();

        }

        //列表详情
        public function index(){


            

            $id = $_GET['id'];
            // session('classify.id',$id);
            $data = M('category')->where("id='$id'")->find();
            if($pname = M('category')->where('id='.$data['pid'])->find()){
                $ppname = M('category')->where('id='.$pname['pid'])->find();
                if (empty($ppname)) {
                    $ppname = null;
                }
            }
            if (empty($pname)){
                $pname = null;
            }
            $data2 = M('category')->order('id desc')->where('pid = '.$id)->select();
            $this->assign('data2',$data2);
            $this->assign('pid',$pname['id']);
            $this->assign('pname',$pname['name']);
            $this->assign('ppname',$ppname['name']);
            $this->assign('classify',$data['name']);
            $this->assign('title','分类');
            $data3 = array();
            foreach ($data2 as $key => $val) {
                $content= M('experience')->order('id desc')->where('category_id = '.$val['id'])->select();
                $content2= M('category')->where('pid = '.$val['id'])->select();
                $data3[] = $content;
                    foreach ($content2 as $key => $value) {
                        $content2 = M('experience')->order('id desc')->where('category_id = '.$value['id'])->select();
                        $data3[] = $content2;
                    }
            }
            $data4 = array();
            foreach ($data3 as $key => $val2) {
                foreach ($val2 as $key => $val3) {
                    $data4[] = $val3;
                }
            }
            $data5 = M('experience')->order('id desc')->where('category_id = '.$id)->select();
            foreach ($data5 as $key => $value) {
                $data4[] = $value;
            }
            

            $this->assign('content',$data4);

            $this->display();
        }

        public function index2()
        {   
            //分页
            $heihei = M('experience');
            $count = $heihei->count();// 查询满足要求的总记录数
            $Page = new \Think\Page($count,8);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $this->lastSuffix = false;
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('first','首页');
            $Page->setConfig('last','尾页');
            $show = $Page->show();// 分页显示输出
            //end 分页
            $data = M('experience')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            $this->assign('content',$data);
            $this->assign('heiheihei','全部分类');
            $this->assign('page',$show);// 赋值分页输出
            $this->display('Classify/index');
        }
        //删除操作
        public function del()
        {
            $data= M('experience')->where('id = '.$_GET['id'])->find();
            
            $picture = $_SERVER["DOCUMENT_ROOT"].$data['picture'];
            $_s_picture = $_SERVER["DOCUMENT_ROOT"].$data['_s_picture'];
            $_l_picture = $_SERVER["DOCUMENT_ROOT"].$data['_l_picture'];

            unlink($picture);
            unlink($_s_picture);
            unlink($_l_picture);
            if(M('experience')->delete($_GET['id']) > 0){
                M('collect')->where(array('eid'=>array('eq',$_GET['id'])))->delete();
                M('comment')->where(array('eid'=>array('eq',$_GET['id'])))->delete();
                M('reply')->where(array('eid'=>array('eq',$_GET['id'])))->delete();
                $this->ajaxReturn(false);
                
            }else{
                $this->ajaxReturn(true);
            }
        }
}
        


